package com.example.myhelloworld.server;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * 
 * @author Habbe
 *
 *
 * The exerciseObject is an Object that holds all possible information about a specific exercise, possible entries should be: 
 * - statistics, i.e. times used, results, possibly all results ever submitted to the server. 
 * - argument to create that object in the produceExercise-class on the client. May store multiple possible arguments and settings for a exercise.    
 * - type of users that the exercise is visible to. 
 * 
 * 
 *  far from finished
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)

public class ExerciseObject {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
//	private String key;
	private Key key;
	
	@Persistent
	String ExerciseName; 
	@Persistent
	String[] exerciseOptions = new String[10];
	@Persistent
	String produceThisOne;
	@Persistent
	Boolean forStudent = false;
	@Persistent
	Boolean forTeacher = false;
	@Persistent
	Boolean forAdmin = false;
	@Persistent
//	int[] results = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//	int[] results = new int[101]; 
	String results = "";
	
	ExerciseObject(String exName, String produceString, String availableTo, String option){
		ExerciseName = exName;
//		key = ExerciseName;
		
		produceThisOne = produceString;
		
		exerciseOptions[0] = option;
		
		if (availableTo.contains("student")){
			forStudent = true;
		}
		if (availableTo.contains("teacher")){
			forTeacher = true;
		}
		if (availableTo.contains("admin")){
			forAdmin = true;
		}
		for (int i = 0; i<101; i++){
//			results[i] = i;
			results = results + "0,";
		} 	
	}
	public String getProduceThisOne() {
		return produceThisOne;
	}
	
	/**
	 * 
	 * @param usertype a string containing a user type to compare with
	 * @return true if this exercise is compatible with the asking user, false otherwise. 
	 */
	public boolean compatibleUsertype(String usertype){
		return (usertype.contains("student") && forStudent) || (usertype.contains("teacher") && forTeacher) || (usertype.contains("admin") && forAdmin); 
	}
	
	
	public void addOption(String newOpt){
		exerciseOptions[exerciseOptions.length] = newOpt; 
	}
	
	public String getOption(){
		return exerciseOptions[0];
	}
	
	public String getRandomOption(){
		//well.. not that random.. but it might be in the future...
		return exerciseOptions[0];
	}

	public Boolean getForStudent() {
		return forStudent;
	}
	
	public void setForStudent(Boolean forStudent) {
		this.forStudent = forStudent;
	}

	public Boolean getForTeacher() {
		return forTeacher;
	}

	public void setForTeacher(Boolean forTeacher) {
		this.forTeacher = forTeacher;
	}

	public Boolean getForAdmin() {
		return forAdmin;
	}

	public void setForAdmin(Boolean forAdmin) {
		this.forAdmin = forAdmin;
	}

	public String getExerciseName() {
		return ExerciseName;
	}
	/**
	 * 
	 * @param result a number from 0 to 100.
	 * @return returns the amount of users that have gotten the supplied result 
	 */
	public int getResult(int result){
		String answer = "";
		String[] resultArray = results.split(",");
		int scoreAmount = Integer.parseInt(resultArray[result]);
		scoreAmount++;
		resultArray[result] = Integer.toString(scoreAmount);
		for (int i = 0; i<resultArray.length; i++){
			answer = answer + resultArray[i] +  ",";  
			
		}
		return scoreAmount;
		
	}
	public int[] getAllResults(){
//		return results;
		int[] answer = new int[101];
		
		String[] fractions = results.split(",");
		for (int i = 0; i < 101; i++){
			answer[i] = Integer.parseInt(fractions[i]);
		}
		return answer;
	}
	
	/**
	 * 
	 * @param result increases the amount of 
	 */
	public void setResultPlusOne(int result){
		String answer = "";
		String[] resultArray = results.split(",");
		int scoreAmount = Integer.parseInt(resultArray[result]);
		scoreAmount++;
		resultArray[result] = Integer.toString(scoreAmount);
		for (int i = 0; i<resultArray.length; i++){
			answer = answer + resultArray[i] +  ",";  
			
		}
		results = answer;
			
//		results[result-1] = results[result-1] + 1; 
	}

}
